package com.vaadin.tutorial.crm.ui.view.list;

import com.vaadin.flow.component.Tag;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.grid.GridVariant;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.data.value.ValueChangeMode;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import com.vaadin.tutorial.crm.backend.entity.SysUser;
import com.vaadin.tutorial.crm.backend.entity.Teacher;
import com.vaadin.tutorial.crm.backend.service.SysUserService;
import com.vaadin.tutorial.crm.ui.MainLayout;


@Route(value = "user-list", layout = MainLayout.class)
@PageTitle("userList")
@Tag("userList-view")
public class UserView extends VerticalLayout {
    private SysUserService userService;
    private UserForm userForm;

    private Grid<SysUser> grid = new Grid<>(SysUser.class);
    private TextField filterText = new TextField();

    public UserView(SysUserService userService) {
        this.userService = userService;
        configureGrid();

        //表单
        userForm = new UserForm();
        userForm.addListener(UserForm.SaveEvent.class, this::saveTeacher);
        userForm.addListener(UserForm.DeleteEvent.class, this::deleteTeacher);
        userForm.addListener(UserForm.CloseEvent.class, e -> closeEditor());

        //将表格、表单添加到一个div
        Div div = new Div(grid, userForm);
        div.addClassName("main");
        div.setSizeFull();

        //将toolbar、div添加到界面
        add(getToolbar(), div);
        updateList("");
        closeEditor();
    }
    
    private void configureGrid() {
        grid.addClassName("user-grid");
        grid.addThemeVariants(GridVariant.LUMO_ROW_STRIPES);
        grid.removeColumnByKey("userId");
        grid.removeColumnByKey("username");
        grid.removeColumnByKey("password");
        grid.removeColumnByKey("userRole");
        grid.removeAllColumns();
//        grid.removeColumnByKey("Account Non Expired");
//        grid.removeColumnByKey("Account Non Locked");
//        grid.removeColumnByKey("Authorities");
//        grid.removeColumnByKey("Credentials Non Expired");
//        grid.removeColumnByKey("Enabled");

//        grid.setSizeFull();
        grid.addColumn("username").setHeader("用户名");
        grid.addColumn("userRole").setHeader("角色");
        grid.getColumns().forEach(col -> col.setAutoWidth(true));

        //点击表格中的某一列添加到表单中
        grid.asSingleSelect().addValueChangeListener(event ->
                editUser(event.getValue()));
    }

    //搜索框和添加按钮作为一个toolbar
    private HorizontalLayout getToolbar() {
        filterText.setPlaceholder("请输入用户名...");
        filterText.setClearButtonVisible(true);
        filterText.setValueChangeMode(ValueChangeMode.LAZY);
        filterText.addValueChangeListener(e -> updateList(e.getValue()));

        Button addUserButton = new Button("新增用户信息");
        addUserButton.addClickListener(click -> addUser());

        HorizontalLayout toolbar = new HorizontalLayout(filterText, addUserButton);
        toolbar.addClassName("toolbar");
        return toolbar;
    }
    
    //更新表格
    private void updateList(String value) {
        grid.setItems(userService.findAll(value));
    }

    //清除表单的值
    private void closeEditor() {
        userForm.setSysUser(null);
        userForm.setVisible(false);
        removeClassName("editing");
    }

    public void editUser(SysUser user) {
        if (user == null) {
            closeEditor();
        } else {
            userForm.setSysUser(user);
            userForm.setVisible(true);
            addClassName("editing");
        }
    }

    //保存
    private void saveTeacher(UserForm.SaveEvent event) {
        userService.save(event.getSysUser());
        updateList("");
        closeEditor();
    }

    //删除
    private void deleteTeacher(UserForm.DeleteEvent event) {
        userService.delete(event.getSysUser());
        updateList("");
        closeEditor();
    }

    void addUser() {
        grid.asSingleSelect().clear();
        editUser(new SysUser());
    }
}
